<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>🧚‍♀️ AI童话书</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
        }

        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            min-height: 100vh;
            color: #333;
        }

        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        .header {
            text-align: center;
            margin-bottom: 30px;
            color: white;
        }

        .header h1 {
            font-size: 2.5em;
            margin-bottom: 10px;
            text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
        }

        .header p {
            font-size: 1.2em;
            opacity: 0.9;
        }

        .main-content {
            background: white;
            border-radius: 15px;
            padding: 30px;
            box-shadow: 0 10px 30px rgba(0,0,0,0.2);
            margin-bottom: 20px;
        }

        .control-panel {
            display: flex;
            gap: 15px;
            margin-bottom: 30px;
            flex-wrap: wrap;
        }

        .btn {
            padding: 12px 24px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            font-weight: bold;
            transition: all 0.3s ease;
            text-decoration: none;
            display: inline-block;
        }

        .btn-primary {
            background: linear-gradient(45deg, #667eea, #764ba2);
            color: white;
        }

        .btn-primary:hover {
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
        }

        .btn-secondary {
            background: #f8f9fa;
            color: #495057;
            border: 2px solid #dee2e6;
        }

        .btn-secondary:hover {
            background: #e9ecef;
            border-color: #adb5bd;
        }

        .template-selector {
            margin-bottom: 20px;
        }

        .template-selector label {
            display: block;
            margin-bottom: 8px;
            font-weight: bold;
            color: #495057;
        }

        .template-selector select {
            width: 100%;
            padding: 12px;
            border: 2px solid #dee2e6;
            border-radius: 8px;
            font-size: 16px;
            background: white;
        }

        .story-display {
            margin-top: 30px;
            padding: 25px;
            background: #f8f9fa;
            border-radius: 12px;
            border-left: 5px solid #667eea;
            display: none;
        }

        .story-title {
            font-size: 1.5em;
            font-weight: bold;
            color: #495057;
            margin-bottom: 15px;
            text-align: center;
        }

        .story-text {
            font-size: 1.1em;
            line-height: 1.8;
            color: #495057;
            margin-bottom: 25px;
            text-align: justify;
        }

        .choices {
            display: flex;
            flex-direction: column;
            gap: 12px;
        }

        .choice-btn {
            padding: 15px 20px;
            background: white;
            border: 2px solid #667eea;
            border-radius: 8px;
            cursor: pointer;
            font-size: 16px;
            transition: all 0.3s ease;
            text-align: left;
        }

        .choice-btn:hover {
            background: #667eea;
            color: white;
            transform: translateX(5px);
        }

        .loading {
            text-align: center;
            padding: 20px;
            color: #6c757d;
        }

        .loading::after {
            content: '';
            animation: dots 1.5s infinite;
        }

        @keyframes dots {
            0%, 20% { content: ''; }
            40% { content: '.'; }
            60% { content: '..'; }
            80%, 100% { content: '...'; }
        }

        .error {
            background: #f8d7da;
            color: #721c24;
            padding: 15px;
            border-radius: 8px;
            margin: 15px 0;
            border: 1px solid #f5c6cb;
        }

        .footer {
            text-align: center;
            color: white;
            opacity: 0.8;
            margin-top: 30px;
        }

        @media (max-width: 768px) {
            .container {
                padding: 15px;
            }
            
            .header h1 {
                font-size: 2em;
            }
            
            .main-content {
                padding: 20px;
            }
            
            .control-panel {
                flex-direction: column;
            }
        }
    </style>
</head>
<body>
    <div class="container">
        <div class="header">
            <h1>🧚‍♀️ AI童话书</h1>
            <p>与AI一起创造属于你的奇幻故事</p>
        </div>

        <div class="main-content">
            <div class="template-selector">
                <label for="storyTemplate">选择故事主题：</label>
                <select id="storyTemplate">
                    <option value="magic_forest">🌲 魔法森林的冒险</option>
                    <option value="underwater">🌊 海底世界探险</option>
                    <option value="space">🚀 太空探险记</option>
                </select>
            </div>

            <div class="control-panel">
                <button class="btn btn-primary" onclick="generateStory()">
                    📖 生成新故事
                </button>
                <button class="btn btn-secondary" onclick="resetStory()">
                    🔄 重新开始
                </button>
            </div>

            <div id="storyDisplay" class="story-display">
                <div id="storyTitle" class="story-title"></div>
                <div id="storyText" class="story-text"></div>
                <div id="choices" class="choices"></div>
            </div>

            <div id="loading" class="loading" style="display: none;">
                正在生成故事
            </div>

            <div id="error" class="error" style="display: none;"></div>
        </div>

        <div class="footer">
            <p>🎭 AI童话书 - 让想象力自由飞翔</p>
        </div>
    </div>

    <script>
        let currentStory = null;

        async function generateStory() {
            const template = document.getElementById('storyTemplate').value;
            const loading = document.getElementById('loading');
            const storyDisplay = document.getElementById('storyDisplay');
            const errorDiv = document.getElementById('error');

            // 显示加载状态
            loading.style.display = 'block';
            storyDisplay.style.display = 'none';
            errorDiv.style.display = 'none';

            try {
                const response = await fetch('/api/generate_story', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ template: template })
                });

                const data = await response.json();

                if (data.success) {
                    currentStory = data.story;
                    displayStory(data.story);
                } else {
                    showError('生成故事失败: ' + data.error);
                }
            } catch (error) {
                showError('网络错误: ' + error.message);
            } finally {
                loading.style.display = 'none';
            }
        }

        function displayStory(story) {
            const storyDisplay = document.getElementById('storyDisplay');
            const storyTitle = document.getElementById('storyTitle');
            const storyText = document.getElementById('storyText');
            const choicesDiv = document.getElementById('choices');

            storyTitle.textContent = story.title;
            storyText.textContent = story.text;

            // 清空并添加选择按钮
            choicesDiv.innerHTML = '';
            if (story.choices && story.choices.length > 0) {
                story.choices.forEach((choice, index) => {
                    const button = document.createElement('button');
                    button.className = 'choice-btn';
                    button.textContent = `${index + 1}. ${choice.text}`;
                    button.onclick = () => continueStory(choice.id);
                    choicesDiv.appendChild(button);
                });
            }

            storyDisplay.style.display = 'block';
        }

        async function continueStory(choiceId) {
            const loading = document.getElementById('loading');
            const errorDiv = document.getElementById('error');

            loading.style.display = 'block';
            errorDiv.style.display = 'none';

            try {
                const response = await fetch('/api/continue_story', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ choice_id: choiceId })
                });

                const data = await response.json();

                if (data.success) {
                    // 添加续集到故事文本
                    const storyText = document.getElementById('storyText');
                    storyText.textContent += '\n\n' + data.continuation;
                    
                    // 隐藏选择按钮
                    const choicesDiv = document.getElementById('choices');
                    choicesDiv.innerHTML = '<p style="text-align: center; color: #6c757d; font-style: italic;">故事完结 🎉</p>';
                } else {
                    showError('继续故事失败: ' + data.error);
                }
            } catch (error) {
                showError('网络错误: ' + error.message);
            } finally {
                loading.style.display = 'none';
            }
        }

        function resetStory() {
            const storyDisplay = document.getElementById('storyDisplay');
            const errorDiv = document.getElementById('error');
            
            storyDisplay.style.display = 'none';
            errorDiv.style.display = 'none';
            currentStory = null;
        }

        function showError(message) {
            const errorDiv = document.getElementById('error');
            errorDiv.textContent = message;
            errorDiv.style.display = 'block';
        }

        // 页面加载完成后的初始化
        document.addEventListener('DOMContentLoaded', function() {
            console.log('🧚‍♀️ AI童话书已加载完成');
        });
    </script>
</body>
</html>